package com.js.rtu.fragment.first;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.js.rtu.R;
import com.js.rtu.RTUApplication;
import com.js.rtu.adapter.DeviceBean;
import com.js.rtu.base.BaseFragment;

import java.util.UUID;

import butterknife.BindView;
import butterknife.OnClick;
import cn.wandersnail.bluetooth.ConnectCallback;
import cn.wandersnail.bluetooth.Connection;
import cn.wandersnail.bluetooth.EventObserver;


/**
 * 蓝牙连接
 */
public class ConnectFragment extends BaseFragment implements  EventObserver{

    public static UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.ll_debug)
    LinearLayout llDebug;

    private Connection connection;
    private DeviceBean bean;
    private static final String ARG_BEAN = "arg_bean";

    public static ConnectFragment newInstance(DeviceBean bean) {
        Bundle args = new Bundle();
        ConnectFragment fragment = new ConnectFragment();
//        args.putSerializable(ARG_BEAN, bean);
        args.putParcelable(ARG_BEAN, bean);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_first_connect;
    }

    @Override
    protected void initView() {
        Bundle bundle = getArguments();
        toolbar.inflateMenu(R.menu.menu_connect);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                    connect();

                return false;
            }
        });
        initToolbarNav(toolbar);
        if (bundle != null) {
            bean = bundle.getParcelable(ARG_BEAN);
//            bean = (DeviceBean) bundle.getSerializable(ARG_BEAN);
            if (bean == null)
                return;
            BluetoothDevice beanDevice = bean.getDevice();
            toolbarTitle.setText(beanDevice.getAddress());
            connection= RTUApplication.getInstance().getConnection(beanDevice);
        }
    }

    public void connect() {
        if (connection == null){
            add("连接失败，找不到设备", llDebug);
            return;
        }

        if (connection.isConnected()) {
            add("已连接", llDebug);
            return;
        }

        add("连接中", llDebug);

        connection.connect(SPP_UUID, new ConnectCallback() {
            @Override
            public void onSuccess() {
                add("连接成功", llDebug);

            }

            @Override
            public void onFail(@NonNull String errMsg, @Nullable Throwable e) {

                add(errMsg, llDebug);
                add("连接断开", llDebug);
//                RTUApplication.getInstance().releaseConnection();
            }
        });
    }




    @OnClick(R.id.tv_clean)
    public void onViewClicked() {
        llDebug.removeAllViews();
    }
}
